const md5 = require('./md5.js')
const app = getApp();

//根据参数获得签名信息
const getReqSign = params => {

  console.info("===getReqSign===params=========");
  console.info(params);

  var paramArr = [];
  for (let p in params) {
    paramArr.push(p);
  } 
  
  //对参数排序
  paramArr = paramArr.sort();
  //拼参数
  let urlParam = "";
  var item = null;
  for (var i = 0; i < paramArr.length;i++) {
    item = paramArr[i];
    if (0 != i)
    {
      urlParam +="&";
    }
    urlParam += item + "=";
    urlParam += encodeURIComponent(params[item]);
  } 

  urlParam += "&app_key=" + app.globalData.aiAppKey;
  let sign = md5.hex_md5(urlParam);
  return sign.toUpperCase();

}

function getRandStr() {
  return Math.random().toString(36).substr(2);
}

//处理微信的待翻译文本，文本中不能有空格，英文?,之类的。
function textHandle(str) {
  //删除空格
  str = str.replace(/\s+/g, "");
  str = str.replace(/[?,]*/g, "");
  return str;
}

module.exports = {
  getReqSign: getReqSign,
  textHandle: textHandle,
  getRandStr: getRandStr
}